<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    cuId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async loadRoles() {
      try {
        // 点击分配角色 获取所有角色
        const res = await getRoles({ page: 1, pagesize: 100 })
        console.log(res, 'getRoles')
        this.list = res.data.rows
        // 点击分配角色 获取权限 数据回填
        const res1 = await getUserDetailById(this.cuId)
        console.log(res1, 'getUserDetailById222')
        this.roleIds = res1.data.roleIds
      } catch (error) {
        console.dir(error)
        this.$message.error('请求数据错误')
      }
    },
    async hSubmit() {
      const res = await assignRoles({ id: this.cuId, roleIds: this.roleIds })
      console.log(res, 'assignRoles')
      this.$emit('success')
    },

    closeDialog() {

    }
  }
}
</script>
